package com.snack.common.utils;

import org.apache.log4j.Logger;


public class Threads {
	protected static final Logger LOG = Logger.getLogger(Threads.class.getName());

	public static Thread setDaemonThreadRunning(Thread t, String name) {
		return setDaemonThreadRunning(t, name, null);
	}

	public static Thread setDaemonThreadRunning(Thread t, String name, Thread.UncaughtExceptionHandler handler) {
		t.setName(name);
		if (handler != null) {
			t.setUncaughtExceptionHandler(handler);
		}
		t.setDaemon(true);
		t.start();
		return t;
	}

	public static void shutdown(Thread t) {
		shutdown(t, 0L);
	}

	public static void shutdown(Thread t, long joinwait) {
		while (t.isAlive()){
			try {
				t.join(joinwait);
			} catch (InterruptedException e) {
				LOG.warn(t.getName() + "; joinwait=" + joinwait, e);
			}
		}
	}
	
	public static void sleep(long millis){
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}
}
